//
//  ClassCell.m
//  IpadR
//
//  Created by edianzu on 2017/5/27.
//  Copyright © 2017年 ruilai. All rights reserved.
//

#import "ClassCell.h"
#import "ClassModel.h"
#import "ChildModel.h"
@interface ClassCell()

@property (copy,nonatomic) NSIndexPath * indexPath;
@property (strong,nonatomic) NSMutableArray * modelArr;

@end

@implementation ClassCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
    }
    self.selectionStyle =UITableViewCellSelectionStyleNone;
    return self;
}

-(void)relayoutCell:(NSMutableArray*)array andIndexPath:(NSIndexPath *)index andAllChildArr:(NSMutableArray *)AllchildArr andProArr:(NSMutableArray *)proArr{
    NSInteger lines ;
    
    
    self.indexPath = index;
    self.modelArr = [[NSMutableArray alloc]initWithArray:array];
//    array;
    lines = [self getCellHeight:array];
    int n = 0;
    for (int i=0; i< lines; i++)
    {
        for (int j=0; j<4; j++)
        {
        _model = array[n];
            ////////////////////////////////算出有多少孩子测试完毕，用来改变图片状态
            NSMutableArray * childOfThisClass=[[NSMutableArray alloc]init];//当前这个小班级的孩子数组
            for (ChildModel * mod in AllchildArr) {
                if ([mod.ClassId isEqual: _model.Id]) {
                    [childOfThisClass addObject:mod];
                }
            }
            NSInteger testedPeople = 0;
            if (proArr.count ==0) {//如果没有项目
                testedPeople = 0;
            }else{
                testedPeople =  [self getTestedBabyWithModelArray:childOfThisClass andProjectArray:proArr];
            }
            
            NSInteger thisclassNUm= 0;
            for (ChildModel * model in childOfThisClass) {
                thisclassNUm += 1;
            }
            
        _backView = [[UIView alloc]initWithFrame:CGRectMake(35*AutoScaleHorizontal+215*AutoScaleHorizontal*j, 20*AutoScaleVertical+ 125*AutoScaleVertical*i, 200*AutoScaleHorizontal, 85*AutoScaleVertical)];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.borderColor = UIColorFromRGB(212, 212, 212).CGColor;
        _backView.layer.borderWidth = 1;
        _backView.tag = n+100;
        [self.contentView addSubview:_backView];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [_backView addGestureRecognizer:tap];

        _grayView = [[UIView alloc]init];
        _grayView.backgroundColor = UIColorFromRGB(242, 242, 242);
        [_backView addSubview:_grayView];
        [_grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(_backView);
            make.size.mas_equalTo(CGSizeMake(200*AutoScaleHorizontal, 25*AutoScaleVertical));
        }];
        
        _checkLabel = [[UILabel alloc]init];
        _checkLabel.textColor = UIColorFromRGB(82, 201, 188);
            _checkLabel.userInteractionEnabled = true;
        _checkLabel.text = @"查看体测情况";
        _checkLabel.font = [UIFont systemFontOfSize:13*AutoScaleHorizontal];
        [_checkLabel setTextAlignment:NSTextAlignmentCenter];
        [_backView addSubview:_checkLabel];
        [_checkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(_backView.mas_bottom).with.offset(-25*AutoScaleVertical);
            make.centerX.mas_equalTo(_backView.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(100*AutoScaleHorizontal, 12*AutoScaleVertical));
        }];
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.text = _model.ClassName;
        _titleLabel.font = [UIFont systemFontOfSize:12*AutoScaleHorizontal];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_grayView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(_grayView.mas_centerY);
            make.centerX.mas_equalTo(_grayView.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(50 *AutoScaleHorizontal, 12*AutoScaleVertical));
        }];
        
        _doneIMG = [[UIImageView alloc]init];
            ///////////图片状态改变
            if (proArr.count==0) {
                _doneIMG.image = [UIImage imageNamed:@"meiyoutupiannibawoza"];
            }else{
            if (testedPeople == 0) {
                _doneIMG.image = [UIImage imageNamed:@"done1"];
            }else if (testedPeople == thisclassNUm){
                _doneIMG.image = [UIImage imageNamed:@"done3"];
            }else{
                _doneIMG.image = [UIImage imageNamed:@"done2"];
            }
            }
        
        [_backView addSubview:_doneIMG];
        [_doneIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(_backView);
            make.size.mas_equalTo(CGSizeMake(34*AutoScaleHorizontal, 35*AutoScaleVertical));
        }];
            
            n++;
            if (n == array.count) {
                break;
            }
    }
    }
}


//方法：每个CELL放四个VIEW，通过数据，算出具体CELL高度
-(NSInteger)getCellHeight:(NSMutableArray*)CellArr{
    
    if (CellArr.count == 0) {
        return 0;
    }else{
        
        float num = CellArr.count/4.0;
        //    NSLog(@"12123  %.2f",num);
        NSInteger lines;
        if (num <= 1) {
            lines = 1;
        }else {
            if (CellArr.count % 4 ==0) {
                lines = CellArr.count /4;
            }else{
                lines = CellArr.count /4 + 1;
            }
        }
        return lines;
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    _action(self.modelArr[tap.view.tag - 100],self.indexPath);
}

//- (void)btnAction:(UIButton *)snder{
//    NSLog(@"1");
//}
#pragma mark 查找以测试人数
//得到已经测试人数的方法
-(NSInteger)getTestedBabyWithModelArray:(NSMutableArray *)modelArr andProjectArray:(NSMutableArray*)projectArr{
    NSInteger testedNum  = 0;
    
    for (int i = 0; i<modelArr.count; i++) {
        ChildModel * model = modelArr[i];
        int a = 0;//完成项目
        int b = 0;//有多少项目
        if (model.IsAbsense) {
            a = a+1;
            b = b+1;
        }else{
            for (NSMutableArray * arr in projectArr) {
                for (NSMutableDictionary * dict in arr) {
                    
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"1"]) {
                        if (model.Height !=nil) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"2"]) {
                        if (model.Weight !=nil) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"3"]) {
                        if ((model.IsMentalShuttleRun )||(model.IsMotorShuttleRun )||(model.ShuttleRun !=nil)) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"4"]) {
                        if ((model.IsMentalSitReach )||(model.IsMotorSitReach )||(model.SitReach !=nil)) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"5"]) {
                        if ((model.IsMentalBroadJump )||(model.IsMotorBroadJump )||(model.BroadJump !=nil)) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"6"]) {
                        if ((model.IsMentalDoubleJump )||(model.IsMotorDoubleJump )||(model.DoubleJump !=nil)) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"7"]) {
                        if ((model.IsMentalThrow )||(model.IsMotorThrow )||(model.Throw !=nil)) {
                            a= a+1;
                        }
                    }
                    if ([[dict objectForKey:@"proNum"]isEqualToString:@"8"]) {
                        if ((model.IsMentalBalance )||(model.IsMotorBalance )||(model.Balance !=nil)) {
                            a= a+1;
                        }
                    }
                    b++;
                }
            }}
        if(a == b){
            testedNum +=1;
        }
    }
    return testedNum;
}

@end


























